%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% O-Ring Production Networks
%   Author: Demir, Fieler, Xu, Yang
%   Last updated: December 2020
%
% Purpose: Estimation (Full Model)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all; diary off; clc; clear; 


%% Solve again using estimates   
   
    % parameters
    define_param
    
    % data moment
    define_data_moment

    % add estiamted parameters 
    load('../../output/estimates/fullmodel_estimates.mat', 'est');            
    define_param_est      
    
    % constants
    define_omega_dist %firm type and density
    define_const %qgrid, phi_v, phy_y, D_F and other constants
    
    % solve initial equilibrium
    eqm = solve_eqm_outer(param, const);    
    define_wage_schedule %add wage schedule
    
    % calculate model moments and objfcn
    model = cal_model_moment(param, const, eqm); 
    bartik = cal_chg_bartik(param, const, eqm);     
    fval = cal_objfcn(data, model, bartik);
        


%% Save figures and tables
diary off

    % create folder
    mkdir('../../output/figures/Figure4') 
    mkdir('../../output/tables') 

    % output results
    out_moments_figures
    out_moments_tables    

    
    